package com.bms.admin.bussiness.company.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.company.entity.Company;
import com.bms.common.bussiness.company.service.ICompanyService;
import com.feilong.core.Validator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 部门管理  前端控制器
 * </p>
 *
 * @author xj
 * @since 2018-08-09
 */
@RestController
@RequestMapping("company")
@Api(value="Company-Api",tags="公司管理")
public class CompanyController extends BaseController {

	@Autowired
	private ICompanyService companyService;
	
	@GetMapping("checkName")
	@ApiOperation(value="检验是否存在公司",notes="1.存在  0.不存在")
	public Result CheckName(@RequestParam String name){
		EntityWrapper<Company> ew = new EntityWrapper<>();
		ew.eq("name", name);
		ew.ne("status", 4);
		int count = companyService.selectCount(ew);
		return json(count);
	}

	@ApiOperation(value="获取公司列表",notes="status 1：待审核  2：正常  3：已拒绝  4：已删除")
	@GetMapping("list")
	@RequiresPermissions("company:list")
    public Result list(@RequestParam(defaultValue = "1",required=false)Integer pageNum,
    				   @RequestParam(defaultValue = "20",required=false)Integer pageSize,
    				   Integer status) {
		EntityWrapper<Company> ew = new EntityWrapper<>();
		ew.orderBy("create_time", false);
		if(Validator.isNotNullOrEmpty(status)){
			ew.eq("status", status);
		} else {
			ew.ne("status", 4);
		}
		return json(companyService.selectPage(new Page<Company>(pageNum,pageSize),ew));
    }


	/**
	 * 根据名字查询公司
	 * @param name
	 * @return
	 */
	@PostMapping("/search")
	public Result search (@RequestParam String name) {
		EntityWrapper wrapper = new EntityWrapper();
		wrapper.like("name", name);
		wrapper.eq("status", 2);

		return json(companyService.selectList(wrapper));
	}

	@ApiOperation("获取公司信息")
	@GetMapping("info/{companyId}")
	@RequiresPermissions("company:info")
    public Result select(@PathVariable Long companyId){
		return json(companyService.selectById(companyId));
    }

	@Validated
	@ApiOperation("新增公司")
	@PostMapping("save")
	@RequiresPermissions("company:save")
	public Result save(@ApiIgnore @LoginUser NowUser nowUser, @RequestBody Company company){
		Date now = new Date();
		company.setCreateTime(now);
		company.setCreateUser(nowUser.getUsername());
		company.setUpdateTime(now);
		companyService.insert(company);
		return success();
	}

	@Validated
	@ApiOperation("修改公司信息")
	@PostMapping("update")
	@RequiresPermissions("company:update")
	public Result update(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody Company company){
		company.setUpdateTime(new Date());
		company.setUpdateUser(nowUser.getUsername());
		companyService.updateById(company);
		return success();
	}

	@ApiOperation("删除公司")
	@DeleteMapping("delete")
	@RequiresPermissions("company:delete")
    public Result delete(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody List<Long> companyIds) {
		for(Long companyId: companyIds){
			Company company = companyService.selectById(companyId);
			company.setStatus(4);
			company.setUpdateTime(new Date());
			company.setUpdateUser(nowUser.getUsername());
			companyService.updateById(company);
		}
		return success();
    }


}
